<?php

namespace App\Http\Controllers\Admin\V1;

use App\Http\Controllers\Controller;
use App\Models\OperationLog;
use App\Models\User;
use App\Transformers\Admin\OperationTransformer;
use Dingo\Api\Http\Response;
use function request;

class OperationLogController extends Controller
{
    /**
     * 操作日志
     *
     * @return Response
     */
    public function index()
    {
        $userId = [];
        if (request()->has('username')) {
            $userId = User::where('username', 'like', '%' . request()->input('username') . '%')->pluck('id');
        }

        $operations = OperationLog::pimp()
            ->when(request()->has('username'), function ($query) use ($userId) {
                $query->whereIn('user_id', $userId);
            })
            ->when((request()->has('startTime') && request()->has('endTime')), function ($query) {
                $query->whereBetween('created_at', [request()->input('startTime'), request()->input('endTime')]);
            })
            ->paginate($this->pageLimit());

        return $this->response->paginator($operations, new OperationTransformer());
    }
}
